/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



package sonia.golf.dao.sqlite;

//~--- non-JDK imports --------------------------------------------------------

import android.content.Context;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import android.util.Log;

import sonia.golf.util.AssertUtil;
import sonia.golf.util.Util;

//~--- JDK imports ------------------------------------------------------------

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;

import java.util.Properties;
import sonia.golf.StrokeCounter;

/**
 *
 * @author Sebastian Sdorra
 */
public class SQLiteHelper extends SQLiteOpenHelper
{

  /** Field description */
  public static final String DATABASE_NAME = "strokeCounter.db";

  /** Field description */
  public static final int DATABASE_VERSION = 1;

  /** Field description */
  public static final String SQL_CREATE_PATH = "/sql/create-db.sql";

  /** Field description */
  public static final String SQL_STATEMENTS_PATH = "/sql/statements.properties";

  //~--- constructors ---------------------------------------------------------

  /**
   * Constructs ...
   *
   *
   * @param context
   */
  public SQLiteHelper(Context context)
  {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    loadStatements();
  }

  //~--- methods --------------------------------------------------------------

  /**
   * Method description
   *
   *
   *
   * @param db
   * @param path
   */
  public void executePath(SQLiteDatabase db, String path)
  {
    InputStream input = null;
    BufferedReader reader = null;

    try
    {
      input = SQLiteHelper.class.getResourceAsStream(path);
      AssertUtil.assertNotNull(input);
      reader = Util.createReader(input);

      String line = reader.readLine();

      while (line != null)
      {
        line = line.trim();

        if (!line.startsWith("#"))
        {
          Log.v(StrokeCounter.LOG_TAG, line);
          db.execSQL(line);
        }

        line = reader.readLine();
      }
    }
    catch (IOException ex)
    {
      Log.e(StrokeCounter.LOG_TAG, ex.getMessage(), ex);
    }
    finally
    {
      Util.close(reader);
      Util.close(input);
    }
  }

  /**
   * Method description
   *
   *
   * @param queryKey
   * @param args
   *
   * @return
   */
  public Cursor executeQuery(String queryKey, String... args)
  {
    String queryString = getQuery(queryKey);

    if ( Log.isLoggable(StrokeCounter.LOG_TAG, Log.DEBUG) )
    {
      Log.d(StrokeCounter.LOG_TAG, "execute query: ".concat(queryString));
    }

    AssertUtil.assertIsNotEmpty(queryString);

    if (args == null)
    {
      args = new String[0];
    }

    return getReadableDatabase().rawQuery(queryString, args);
  }

  /**
   * Method description
   *
   *
   * @param db
   */
  @Override
  public void onCreate(SQLiteDatabase db)
  {
    executePath(db, SQL_CREATE_PATH);
  }

  /**
   * Method description
   *
   *
   * @param db
   * @param i
   * @param i1
   */
  @Override
  public void onUpgrade(SQLiteDatabase db, int i, int i1)
  {

    // nothing todo
  }

  //~--- get methods ----------------------------------------------------------
  /**
   * Method description
   *
   *
   * @param queryKey
   *
   * @return
   */
  public String getQuery(String queryKey)
  {
    AssertUtil.assertIsNotEmpty(queryKey);

    return statements.getProperty(queryKey);
  }

  //~--- methods --------------------------------------------------------------

  /**
   * Method description
   *
   */
  private void loadStatements()
  {
    InputStream input = null;

    try
    {
      input = SQLiteHelper.class.getResourceAsStream(SQL_STATEMENTS_PATH);
      AssertUtil.assertNotNull(input);
      statements = new Properties();
      statements.load(input);
    }
    catch (IOException ex)
    {
      Log.e(StrokeCounter.LOG_TAG, ex.getMessage(), ex.getCause());
    }
    finally
    {
      Util.close(input);
    }
  }

  //~--- fields ---------------------------------------------------------------

  /** Field description */
  private SQLiteDatabase database;

  /** Field description */
  private Properties statements;
}
